﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class HoiNghiKHKT
    {
        public String ID;
        public String MaHoiNghiKHKT;
        public String TenHoiNghi;
        public DateTime ThoiGian;
        public String DiaDiem;
        public String NoiDungChuyenMon;
        public String MaBaoCaoCanBo;

        /// <summary>
        /// Properties to get all BaoCaoCanBo have exactly MaBaoCaoCanBo
        /// </summary>
        public List<BaoCaoCanBo> DSBaoCaoCanBo;
        public List<BaoCaoCanBo> getListBaoCaoCanBo()
        {
            return BaoCaoCanBo.getAllBaoCaoCanBo(MaBaoCaoCanBo);
        }
        /// <summary>
        /// Constructor to get all the informations from the db that match ID specified
        /// </summary>
        /// <param name="_ID">ID of the DataRow</param>
        public HoiNghiKHKT(String _ID)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM HoiNghiKHKT WHERE ID = '" + _ID + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = _ID;
                TenHoiNghi = row["TenHoiNghi"].ToString();
                MaHoiNghiKHKT = row["MaHoiNghiKHKT"].ToString();
                ThoiGian = row.Field<DateTime>("ThoiGian");
                DiaDiem = row["DiaDiem"].ToString();
                NoiDungChuyenMon = row["NoiDungChuyenMon"].ToString();
                MaBaoCaoCanBo = row["MaBaoCaoCanBo"].ToString();
            }
        }

        public HoiNghiKHKT(String _TenHoiNghi, DateTime _ThoiGian, String _DiaDiem,
            String _NoiDungChuyenMon, List<BaoCaoCanBo> _DSBaoCaoCanBo)
        {
            ID = Database.getID("HoiNghiKHKT", "ID");
            TenHoiNghi = _TenHoiNghi;
            MaHoiNghiKHKT = Database.getID("HoiNghiKHKT", "MaHoiNghiKHKT");
            ThoiGian = _ThoiGian;
            DiaDiem = _DiaDiem;
            NoiDungChuyenMon = _NoiDungChuyenMon;
            

            //obj
            DSBaoCaoCanBo = _DSBaoCaoCanBo;
            MaBaoCaoCanBo = _DSBaoCaoCanBo[0].MaBaoCaoCanBo;
        }

        public Boolean Save()
        {

           

            QueryResult result;
            //Insert
            result = Database.shareDatabase.Query("INSERT INTO HoiNghiKHKT VALUES(" +
                "'" + ID + "', " +
                "'" + MaHoiNghiKHKT + "', " +
                "'" + Database.sqlDateTime(ThoiGian) + "', " +
                "N'" + DiaDiem + "', " +
                "N'" + NoiDungChuyenMon + "', " +
                "'" + MaBaoCaoCanBo + "', " +
                "N'" + TenHoiNghi + "')");
            if (result.ReturnCode == 200)
            {
                foreach (BaoCaoCanBo bccb in DSBaoCaoCanBo)
                    bccb.Save();
                return true;
            }
            else
                return false;

        }
        public Boolean Update()
        {
            QueryResult result;
            if (Database.isDupplicate("HoiNghiKHKT", "ID", ID))
            {
                //Update
                result = Database.shareDatabase.Query("UPDATE HoiNghiKHKT SET " +
                    "MaHoiNghiKHKT = '" + MaHoiNghiKHKT + "', " +
                    "TenHoiNghi = N'" + TenHoiNghi + "', " +
                    "ThoiGian = '" + Database.sqlDateTime(ThoiGian) + "', " +
                    "DiaDiem = N'" + DiaDiem + "', " +
                    "NoiDungChuyenMon = N'" + NoiDungChuyenMon + "', " +
                    "MaBaoCaoCanBo = '" + MaBaoCaoCanBo + "' WHERE ID = '" + ID + "'");
                if (result.ReturnCode == 200)
                {

                    return true;
                }
                else
                    return false;
            }
            return false;
        }
        /// <summary>
        /// First delete all related data from the BaoCaoCanBo Table and then delete the row
        /// </summary>
        /// <returns></returns>
        public Boolean Delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM HoiNghiKHKT " +
            "WHERE MaBaoCaoCanBo = '" + MaBaoCaoCanBo + "'");
            if (result.ReturnCode == 200)
            {
                foreach (BaoCaoCanBo baoCao in DSBaoCaoCanBo)
                {
                    baoCao.Delete();
                }
                return true;
            }
            else
                return false;
        }

        public static List<HoiNghiKHKT> getAllHoiNghiKHKT(String _MaHoiNghiKHKT)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM HoiNghiKHKT " +
                "WHERE MaHoiNghiKHKT = '" + _MaHoiNghiKHKT + "'");
            if (result.ReturnCode == 200)
            {
                List<HoiNghiKHKT> _result = new List<HoiNghiKHKT>();
                foreach (DataRow dtr in result.Table.Rows)
                    _result.Add(new HoiNghiKHKT(dtr["ID"].ToString()));
                return _result;
            }
            else
                return null;
        }
    }
}
